<template>
  <d2-container>
    <div style="margin: 20px; width: 400px">
      <div style="margin: 20px 0">
        <span style="text-align: left; font-size: 28px; color: #1E90FF"><b>| </b></span>
        <span style="text-align: left; font-size: 22px"><b>项目信息</b></span>
      </div>
      <el-form label-width="80px" size="large" :model="form" :rules="rules" ref="projectForm">
        <el-form-item label="项目类型" prop="type"
                      :rules="[{ required: true, message: '请选择项目类型', trigger: 'change' }]">
          <el-select clearable v-model="form.type" placeholder="请选择" style="width: 50%" :disabled="isDisabled">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目描述" prop="description">
          <el-input type="textarea" v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" autocomplete="off" @click="submit()">完成创建</el-button>
        </el-form-item>
      </el-form>
    </div>
  </d2-container>
</template>

<script>
import { AddObj } from './api.js'

export default {
  name: 'index',
  data () {
    return {
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
          { min: 3, max: 20, message: '项目名称需要多于3个字符但不能超过20个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入项目描述', trigger: 'blur' },
          { min: 5, max: 200, message: '项目描述需要多于5个字符但不能超过200个字符', trigger: 'blur' }
        ]
      },
      isDisabled: false,
      typeList: [
        { id: 0, name: '图像分类' },
        { id: 1, name: '目标检测' },
        { id: 2, name: '图像分割' }
      ]
    }
  },
  methods: {
    // 创建项目
    submit () {
      this.$refs.projectForm.validate((valid) => {
        if (valid) {
          AddObj(this.form).then(response => {
            this.$message.success('创建项目成功')
            this.$router.push({ path: '/model/my' })
          }).catch(error => {
            this.$message.error('创建项目失败')
            console.log(error)
          })
        } else {
          return false
        }
      })
    }
  },
  // 获取路由参数 项目类型
  created: function () {
    const type = this.$route.query.type
    if (typeof (type) === 'undefined') {
      this.isDisabled = false
    } else {
      this.isDisabled = true
      this.form.type = type * 1
    }
  }
}
</script>

<style scoped>
</style>
